package 第六周;

/*
 * @author  YxinMiracle
 * @date  2022-05-04 18:21
 * @Gitee: https://gitee.com/yxinmiracle
 */

public class 跳跃游戏2 {


    public int jump(int[] nums) {
        int length = nums.length - 1;
        int index = 0;
        int ans = 0;
        while (index < length){
            int right = index + nums[index];
            if (right > length) return ans + 1;
            int nextRight = right;
            int next = index;
            for (int i = index+1; i <= right; i++) {
                if (i + nums[i] > nextRight){
                    nextRight = i + nums[i];
                    next = i;
                }
            }
            index = next;
            ans++;
        }

        return ans;
    }

}
